package me.helllp.dake.iam.core.dao.impl;

import me.helllp.dake.core.IdGenerator;
import me.helllp.dake.core.bean.DakeAuthAccount;
import me.helllp.dake.core.utils.PoUtil;
import me.helllp.dake.iam.core.bo.TenantBo;
import me.helllp.dake.iam.core.convert.ApplicationConvert;
import me.helllp.dake.iam.core.convert.TenantConvert;
import me.helllp.dake.iam.core.po.DakeApplication;
import me.helllp.dake.iam.core.po.DakeTenant;
import me.helllp.dake.iam.core.mapper.DakeTenantMapper;
import me.helllp.dake.iam.core.dao.IDakeTenantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dake
 * @since 2022-12-05
 */
@Service
public class DakeTenantServiceImpl extends ServiceImpl<DakeTenantMapper, DakeTenant> implements IDakeTenantService {
    @Autowired
    private TenantConvert tenantConvert;

    @Autowired
    private IdGenerator idGenerator;

    @Override
    public TenantBo create(TenantBo tenantBo, DakeAuthAccount account) {
        Assert.notNull(account, "当前用户不存在");
        Assert.notNull(account.getUserAccount(), "当前用户不存在");
        Assert.notNull(tenantBo, "租户信息为NULL");
        Assert.notNull(tenantBo.getTenantCode(), "租户编码为NULL");
        Assert.notNull(tenantBo.getTenantName(), "租户名称为NULL");

        DakeTenant tenant = this.tenantConvert.toTenant(tenantBo);
        tenant.setId(idGenerator.nextId());
        PoUtil.setInsertPo(tenant, account.getUserAccount());
        save(tenant);
        return this.tenantConvert.toTenantBo(tenant);
    }

    @Override
    public Boolean delete(Long id) {
        return Boolean.valueOf(removeById(id));
    }

    @Override
    public Boolean edit(TenantBo tenantBo, DakeAuthAccount account) {
        DakeTenant tenant = this.tenantConvert.toTenant(tenantBo);
        PoUtil.setUpdatePo(tenant, account.getUserAccount());
        return Boolean.valueOf(updateById(tenant));
    }
}
